package com.example.boos.stores;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.baidu.mapapi.model.LatLng;
import com.bumptech.glide.Glide;
import com.example.boos.HttpUtil.MyPost;
import com.example.boos.R;
import com.example.boos.context.BossInfo;
import com.example.boos.pojo.Store;
import com.example.boos.pojo.StoreAddress;
import com.example.boos.util.MyThreadPool;

import java.util.List;

public class storeItemAdapter extends BaseAdapter {
    private  List<Store> stores;
    private Context context;

    public storeItemAdapter(List<Store> data,Context context){
        stores = data;
        this.context = context;
    }

    public void updateAll(List<Store> data){
        stores = data;
        notifyDataSetChanged();
    }

    public void update(){
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return stores.size();
    }

    @Override
    public Object getItem(int position) {
        return stores.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder vh = null;
        if(convertView == null){
            convertView = View.inflate(context, R.layout.store_item,null);
            vh = new ViewHolder(convertView);
            convertView.setTag(vh);
        }else{
            vh = (ViewHolder) convertView.getTag();
        }
        // 绑定数据
        Store store = stores.get(position);
        Glide.with(convertView).load(store.getStoreImage()).into(vh.storeImage);
        vh.storeName.setText(store.getStoreName());
        if(store.getStoreStatus() == 0){
            vh.btn_removeStore.setEnabled(false);
            vh.storeStatus.setText("已删除");
        }else{
            vh.btn_removeStore.setEnabled(true);
            vh.storeStatus.setText("未删除");
            // 进入详细页面 监听事件
            // 删除店铺监听事件
            View.OnClickListener listener = new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // 进入主页面
                    BossInfo.currentStore = store;
                    BossInfo.currentStoreId = store.getId();
                    // 获取到选中店铺的地址信息  需要开启服务把
                    MyThreadPool.doJob(new Runnable() {
                        @Override
                        public void run() {
                            String url = "getStoreAddress";
                            BossInfo.currentStoreAddress = (StoreAddress) MyPost.doPost(url,store.getId());
                            BossInfo.cur = new LatLng(Double.parseDouble(BossInfo.currentStoreAddress.getLatitude()),
                                    Double.parseDouble(BossInfo.currentStoreAddress.getLongitude()));
                            Message msg = new Message();
                            msg.what = 3;
                            stores_activity.handler.sendMessage(msg);
                        }
                    });

                }
            };
            vh.storeName.setOnClickListener(listener);
            vh.storeImage.setOnClickListener(listener);
            vh.btn_removeStore.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // 删除店铺
                    new AlertDialog.Builder(v.getContext())
                            .setTitle("确认删除该店铺？")
                            .setMessage("确定吗")
                            .setPositiveButton("是", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    // 发送删除请求，更改状态即可
                                    MyThreadPool.doJob(new Runnable() {
                                        @Override
                                        public void run() {
                                            Boolean isRemove = (Boolean) MyPost.doPost("delStore",store.getId());
                                            if(isRemove){
                                                store.setStoreStatus(0);
                                                // remove 掉
                                                stores.remove(position);
                                            }
                                            Message msg = new Message();
                                            msg.what = 1;
                                            msg.obj = isRemove;
                                            stores_activity.handler.sendMessage(msg);
                                        }
                                    });
                                }
                            })
                            .setNegativeButton("否", null)
                            .show();
                }
            });
        }

        return convertView;
    }

    class ViewHolder{
        Button btn_removeStore ;
        TextView storeName;
        ImageView storeImage;
        TextView storeStatus;

        public ViewHolder(View view){
            btn_removeStore = view.findViewById(R.id.btn_removeStore);
            storeName = view.findViewById(R.id.storeName);
            storeStatus = view.findViewById(R.id.storeStatus);
            storeImage = view.findViewById(R.id.storeImage);
        }

    }
}
